<h2>Detailed Receivables</h2>
<h2><?php echo $owner->getListName()?></h2>
<h3>as of <?php echo date('m/d/Y',strtotime($asof_date))?></h3>
<?php $total_invoice = 0;?>
<?php $total_debit = 0;?>
<?php $total_credit = 0;?>
<?php $total_payment = 0;?>
<?php $total_others = 0;?>
<?php $total_receivables = 0;?>
<?php $total_notdue = 0?>
<?php $total_due30 = 0?>
<?php $total_due60 = 0?>
<?php $total_due90 = 0?>
<?php $total_due120 = 0?>
<?php $total_overdue = 0?>
<table class="form">
  <tr>
    <th>Customer Name:</th>
    <td><?php echo $contact->getListName()?></td>
  </tr>
  <tr>
    <th>Address:</th>
    <td><?php echo nl2br($contact->getCityAddress())?></td>
  </tr>
  <tr>
    <th>Phone:</th>
    <td><?php echo $contact->getPhone()?></td>
  </tr>
</table>
<hr/>
<table class="datagrid">
  <thead class="datagrid">
    <tr>
      <th>Invoice Date</th>
      <th>Due Date</th>
      <th>Invoice#</th>
      <th>Invoice<br/>Amount</th>
      <th>Debit</th>
      <th>Credit</th>
      <th>Payment</th>
      <th>Others</th>
      <th>Balance</th>
      <th>Not Due</th>
      <th>Overdue 30</th>
      <th>Overdue 60</th>
      <th>Overdue 90</th>
      <th>Overdue 120 up</th>
      <th>Total Overdue</th>
    </tr>
  </thead>
  <tbody class="datagrid" style="height:auto;">
    <?php foreach($detailed_receivables as $detailed_receivable):?>
      <?php
        $invoice = InvoicePeer::retrieveByPk($detailed_receivable['invoice_iden']);
        $transaction_list = $invoice->getTransactions();
      ?>
      <tr>
        <td><?php echo $detailed_receivable['document_date']?></td>
        <td><?php echo $detailed_receivable['due_date']?></td>
        <td><?php echo $detailed_receivable['invoice_type'].'-'.$detailed_receivable['invoice_id']?></td>
        <td style="text-align:right;"><?php echo number_format($invoice->getTotal(),2)?></td>
        <td style="text-align:right;"><?php echo number_format($transaction_list['DEBIT MEMO'],2)?></td>
        <td style="text-align:right;"><?php echo number_format($transaction_list['CREDIT MEMO'],2)?></td>
        <td style="text-align:right;"><?php echo number_format($transaction_list['PAYMENT'],2)?></td>
        <td style="text-align:right;"><?php echo number_format($transaction_list['OTHERS'],2)?></td>
        <td style="text-align:right;"><?php echo number_format($detailed_receivable['total'],2)?></td>
        <td style="text-align:right;"><?php echo number_format($detailed_receivable['total_notdue'],2)?></td>
        <td style="text-align:right;"><?php echo number_format($detailed_receivable['total_due30'],2)?></td>
        <td style="text-align:right;"><?php echo number_format($detailed_receivable['total_due60'],2)?></td>
        <td style="text-align:right;"><?php echo number_format($detailed_receivable['total_due90'],2)?></td>
        <td style="text-align:right;"><?php echo number_format($detailed_receivable['total_due120'],2)?></td>
        <?php $overdue = $detailed_receivable['total_notdue']+$detailed_receivable['total_due30']+$detailed_receivable['total_due60']+$detailed_receivable['total_due90']+$detailed_receivable['total_due120'];?>
        <td style="text-align:right;"><?php echo number_format($overdue,2)?></td>
      </tr>
      <?php $total_invoice += $invoice->getTotal();?>
      <?php $total_debit += $transaction_list['DEBIT MEMO'];?>
      <?php $total_credit += $transaction_list['CREDIT MEMO'];?>
      <?php $total_payment += $transaction_list['PAYMENT'];?>
      <?php $total_others += $transaction_list['OTHERS'];?>
      <?php $total_receivables += $detailed_receivable['total'];?>
      <?php $total_notdue += $detailed_receivable['total_notdue']?>
      <?php $total_due30 += $detailed_receivable['total_due30']?>
      <?php $total_due60 += $detailed_receivable['total_due60']?>
      <?php $total_due90 += $detailed_receivable['total_due90']?>
      <?php $total_due120 += $detailed_receivable['total_due120']?>
      <?php $total_overdue += $overdue?>
    <?php endforeach;?>
  </tbody>
  <tfoot class="datagrid">
      <tr>
        <td colspan="3">TOTAL</td>
        <td style="text-align:right;"><?php echo number_format($total_invoice,2) ?></td>
        <td style="text-align:right;"><?php echo number_format($total_debit,2) ?></td>
        <td style="text-align:right;"><?php echo number_format($total_credit,2) ?></td>
        <td style="text-align:right;"><?php echo number_format($total_payment,2) ?></td>
        <td style="text-align:right;"><?php echo number_format($total_others,2) ?></td>
        <td style="text-align:right;"><?php echo number_format($total_receivables,2) ?></td>
        <td style="text-align:right;"><?php echo number_format($total_notdue,2)?></td>
        <td style="text-align:right;"><?php echo number_format($total_due30,2)?></td>
        <td style="text-align:right;"><?php echo number_format($total_due60,2)?></td>
        <td style="text-align:right;"><?php echo number_format($total_due90,2)?></td>
        <td style="text-align:right;"><?php echo number_format($total_due120,2)?></td>
        <td style="text-align:right;"><?php echo number_format($total_overdue,2)?></td>
      </tr>
  </tfoot>
</table>
